package com.dhcc.sdc.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhcc.sdc.cdss.dao.*;
import com.dhcc.sdc.cdss.entity.*;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.dao.CdssRecommendKeywordMapper;
import com.dhcc.sdc.base.service.ICdssRecommendKeywordService;
import com.dhcc.sdc.base.entity.CdssRecommendKeywordEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 表数据服务层接口
 * 
 * @ClassName: CdssRecommendKeywordServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-07-05 09:59:02
 */
@Service
public class CdssRecommendKeywordServiceImpl extends BaseServiceImpl<CdssRecommendKeywordMapper, CdssRecommendKeywordEntity> implements ICdssRecommendKeywordService {

	@Resource
	private SdsQcentityMapper sdsQcentityMapper;
	@Resource
	private SdcCdssItemMapper sdcCdssItemMapper;

	@Override
	public List<CdssRecommendKeywordEntity> page(Page<CdssRecommendKeywordEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdssRecommendKeywordEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importKeyword(List<List<Object>> all) {
		Map<String, Long> qcEntiytMap = new HashMap<>();
		for (int i = 1; i < all.size(); i++){
			List<Object> ls = all.get(i);
			//病种
			String qcAbbrev = ls.get(0).toString();
			//模块
			String recommendType = ls.get(1).toString();
			//推荐方案
			String recommendName = ls.get(2).toString();
			//关键字
			String keyword = ls.get(3).toString();
			//备注
			String remarks = "";
			if (ls.get(4) != null){
				remarks = ls.get(4).toString();
			}

			// 获取病种信息
			SdsQcentityEntity qcEntity = new SdsQcentityEntity();
			qcEntity.setAbbrev(qcAbbrev);
			qcEntity = this.sdsQcentityMapper.selectOne(qcEntity);
			if (qcEntity == null) {
				continue;
			}
			Long qcentityId = qcEntity.getId();
			Long cdssRecommendId = null;
			String itemId = "";
			Integer itemType = 0;
			switch (recommendType){
				case "推荐评估量表":
					itemType = 1;
					break;
				case "推荐检查":
					itemType = 2;
					break;
				case "推荐检验":
					itemType = 3;
					break;
				case "推荐护理处置":
					itemType = 5;
					break;
				case "推荐治疗方案":
					itemType = 4;
					break;
			}

			itemId = sdcCdssItemMapper.selectItemId(recommendName, itemType, qcentityId);
			//cdssRecommendId = sdcCdssItemMapper.selectRecommendId(recommendName, itemType, qcentityId);
			if (StringUtils.isEmpty(itemId)){
				continue;
			}
			CdssRecommendKeywordEntity cdssRecommendKeywordEntity = new CdssRecommendKeywordEntity();
			cdssRecommendKeywordEntity.setQcentityId(qcentityId);
			//cdssRecommendKeywordEntity.setCdssRecommendId(cdssRecommendId);
			cdssRecommendKeywordEntity.setItemId(itemId);
			cdssRecommendKeywordEntity.setRecommendType(recommendType);
			CdssRecommendKeywordEntity cdssRecommendKeywordEntity1 = this.baseMapper.selectOne(cdssRecommendKeywordEntity);
			if (StringUtils.isEmpty(cdssRecommendKeywordEntity1)){
				cdssRecommendKeywordEntity.setKeyword(keyword);
				cdssRecommendKeywordEntity.setRemarks(remarks);
				cdssRecommendKeywordEntity.insert();
			}else {
				cdssRecommendKeywordEntity1.setKeyword(keyword);
				cdssRecommendKeywordEntity1.setRemarks(remarks);
				cdssRecommendKeywordEntity1.updateAllColumnById();
			}
		}
	}

	@Override
	public String getItemIdByType(Long cdssRecommendId, String matchType) {
		String itemId = "";
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(cdssRecommendId);
		if (!StringUtils.isEmpty(sdcCdssItemEntity)){
			itemId = sdcCdssItemEntity.getItemId();
		}
		/*switch (matchType){
			case "推荐评估量表":
				SdcCdssEvalEntity cdssEvalEntity = cdssEvalMapper.selectById(cdssRecommendId);
				itemId = cdssEvalEntity.getItemId();
				break;
			case "推荐检查":
				SdcCdssExamEntity cdssExamEntity = cdssExamMapper.selectById(cdssRecommendId);
				itemId = cdssExamEntity.getItemId();
				break;
			case "推荐检验":
				SdcCdssLabEntity cdssLabEntity = cdssLabMapper.selectById(cdssRecommendId);
				itemId = cdssLabEntity.getItemId();
				break;
			case "推荐护理处置":
				SdcCdssNursingEntity cdssNursingEntity = cdssNursingMapper.selectById(cdssRecommendId);
				itemId = cdssNursingEntity.getItemId();
				break;
			case "推荐治疗方案":
				SdcCdssTreatEntity cdssTreatEntity = cdssTreatMapper.selectById(cdssRecommendId);
				itemId = cdssTreatEntity.getItemId();
				break;
		}*/
		return itemId ;
	}

}